package com.loyi.cloud.hbsystem.cqi.rest;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.loyi.cloud.hbsystem.cqi.utils.CQITotalService;

@RestController
@RequestMapping("/")
public class CQITotalController {

	@Autowired
	private CQITotalService cqiTotalService;

	/**
	 * 
	 * @param datatime
	 * @param type
	 *            汇总类型，0=小时报，1=日报，2=周报，3=月报，4=季报，5=半年报，6=年报
	 */
	@RequestMapping(value = "total", method = { RequestMethod.POST, RequestMethod.GET })
	public void total(
			@RequestBody(required = false) @RequestParam(value = "datatime", required = false) @DateTimeFormat(pattern = "yyyyMMddHHmmss") Date datatime,
			@RequestBody @RequestParam("type") int type) {
		new CQITotalThread(datatime == null ? new Date() : datatime, type).run();

	}

	class CQITotalThread implements Runnable {

		private Date datatime;
		private int type;

		public CQITotalThread(Date datatime, int type) {
			this.datatime = datatime;
			this.type = type;
		}

		public void run() {
			cqiTotalService.execute(datatime, type);
		}
	}

	@RequestMapping(value = "status", method = { RequestMethod.GET })
	public Map<String, Object> status() {
		Map<String, Object> status = new HashMap<String, Object>();
		status.put("isRunning", cqiTotalService.isRunning());
		status.put("finish", cqiTotalService.getFinish());
		status.put("total", cqiTotalService.getTotal());
		status.put("starttime", cqiTotalService.getStarttime());
		status.put("runtime", System.currentTimeMillis() - cqiTotalService.getStarttime());
		return status;
	}

}
